
<!-- 里程明细 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="numberPlate">
        <el-input v-model="queryParams.numberPlate" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="所属公司" prop="companyId">
        <el-tree-select v-model="queryParams.companyId" placeholder="请选择所属公司" clearable :data="treeList"
          :expand-on-click-node="false" :render-after-expand="false" @change="handleQuery" check-strictly
          :props="{ label: 'label' }" node-key="id" filterable />
      </el-form-item>
      <el-form-item label="统计时间" style="width: 438px;">
        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD HH:mm:ss" type="datetimerange" :default-time="[new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),]" range-separator="-" start-placeholder="开始日期"
          end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['statisticalReportForm:mileageDetail:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table highlight-current-row v-loading="loading" :data="tableList">
      <template v-for="v in tableItems">
        <el-table-column :label="v.name" align="left" :prop="v.prop" :width="v.width" />
      </template>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)"
            v-hasPermi="['statisticalReportForm:mileageDetail:track']">查看轨迹</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>
<script setup lang="ts" name="MileageDetail">
import dayjs from "dayjs"
import { reactive, toRefs, ref, getCurrentInstance, onActivated } from "vue"
import { treeDept } from "../../api/system/dept";
import useAppStore from '../../store/modules/app'
const appStore = useAppStore();
import request from '../../utils/request'
const loading = ref(true);
const tableList = ref([]);
const treeList = ref([]);
const dateRange = ref([dayjs(new Date()).format("YYYY-MM-DD 00:00:00"), dayjs(new Date()).format("YYYY-MM-DD 23:59:59")]);
const { proxy } = getCurrentInstance();
const tableItems = ref([
  { name: '设备名称', prop: "numberPlate" },
  { name: '所属公司', prop: "companyName", width: 500 },
  { name: '行驶里程(km)', prop: "kilometer" },
  { name: '行驶时长(min)', prop: "driverMinutes" },
  { name: '统计日期', prop: "day", type: 'time' },
]);
const total = ref(0);
const showSearch = ref(true);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 15,
    numberPlate: null,
    companyId: null,
  },
});
treeDept().then(res => {
  treeList.value = res.data
})
const { queryParams } = toRefs(data);

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  //@ts-ignore
  // proxy.resetForm("queryRef");
  queryParams.value = {
    pageNum: 1,
    pageSize: 15,
    numberPlate: null,
    companyId: null,
  }
  dateRange.value = [dayjs(new Date()).format("YYYY-MM-DD 00:00:00"), dayjs(new Date()).format("YYYY-MM-DD 23:59:59")];
  handleQuery();
}
/** 查询列表 */
function getList() {
  loading.value = true;
  request({
    url: '/comlbs/reportStatistics/deviceMileage/detailList',
    method: 'post',
    //@ts-ignore
    params: proxy.addDateRange2(queryParams.value, dateRange.value)
  }).then(({ data }: any) => {
    tableList.value = data.list;
    total.value = data.total;
    loading.value = false;
  }).catch(err => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  //@ts-ignore
  proxy.download('comlbs/reportStatistics/deviceMileage/detailListExport',
    //@ts-ignore
    proxy.addDateRange2(queryParams.value, dateRange.value)
    , `里程明细_${dayjs(new Date()).format("YYYY-MM-DD_HH:mm:ss")}.xlsx`)
}

/** 详情按钮操作 */
function handleDetail(obj: any) {
  let queryTime = [dayjs(obj.day).add(-60, "minutes").format("YYYY-MM-DD HH:mm:ss"), dayjs(obj.day).format("YYYY-MM-DD HH:mm:ss")];
  window.open(window.location.origin + "/trajectoryTrack?numberPlate=" + obj.numberPlate + "&deviceNo=" + obj.dvo + "&companyId=" + obj.companyId + "&queryTime=" + queryTime)
}

function handlePageQuery(firstIn: Boolean) {
  // 第一次进入或者参数改变需要查询表格
  let query = appStore.pageQuery
  if (query) {
    dateRange.value = query.dateRange;
    queryParams.value.numberPlate = query.numberPlate;
    queryParams.value.companyId = query.companyId;
    appStore.clearPageQuery();
    firstIn = true;
  }
  if (firstIn) {
    getList();
  }
}

onActivated(() => {
  handlePageQuery(false);
})

handlePageQuery(true);
</script>
<style lang='less' scoped></style>